package game 
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.FP;
	
	import utils.SoundBoard;
	
	/**
	 * ...
	 * @author Matt Hofmann
	 */
	public class Weapon extends Entity 
	{
		
		[Embed(source = "../../assets/items.png")]
		private const imgWeapon:Class;
		public var sprWeapon:Spritemap = new Spritemap(imgWeapon, 25, 25 /*, spriteAnimationEnd*/);
		
		public static const KUNAI:uint = 0;
		public static const SHURIKEN:uint = 1;
		public static const PINWHEEL:uint = 2;
		
		// Static constants
		private var mspeed:Number = 15;		// Maximum move speed
		private var lifetime:int =  50;		// Number of ticks before weapon "dies"
		
		// Weapon properties
		private var xspeed:Number = 0;
		private var yspeed:Number = 0;
		private var direction:int = 0;
		private var weapontype:uint = 0;
			
		public function Weapon(wtype:uint, startx:uint, starty:uint, directionx:int) 
		{
			sprWeapon.add("kunai", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 30, true);
			sprWeapon.add("shuriken", [12, 13, 14, 15, 16], 15, true);
			sprWeapon.add("pinwheel", [24, 25, 26, 27, 28, 29, 30], 15, true);
		
			// Set weapon image graphics
			graphic = sprWeapon;
			
			// Define hitbox - slightly smaller than 25x25 sprite size
			setHitbox(23, 21, -1, -2);
			
			// Init weapon coordinates
			this.x = startx;
			this.y = starty;
			this.direction = directionx;
			this.weapontype = wtype;
		}
		
		override public function update():void
		{
			sprWeapon.flipped = direction > 0 ? false : true;
			// Sprite animation
			if (weapontype == KUNAI) {
				sprWeapon.play("kunai", false); 
			}
			else if (weapontype == SHURIKEN) {
				sprWeapon.play("shuriken", false); 
			}
			else if (weapontype == PINWHEEL) {
				sprWeapon.play("pinwheel", false); 
			}
			
			// Movement and collision
			for (var i:int = 0; i < Math.abs(mspeed); i += 1) {
				if (!collide("wall", x + direction, y)) { 
					x += direction; 
				} 
				else { 
					this.destroy();
					break; 
				}
			}
			
			lifetime--;
			if (lifetime <= 0) {
				this.destroy();
			}
		}
		
		public function destroy():void
		{
			// Here we could place specific destroy-behavior for the Weapon.
			FP.world.remove(this);
		}
	}

}